package kr.kwakyc.puzzle;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import kr.kwakyc.puzzle.ranking.RankingDatabase;
import kr.kwakyc.puzzle.ranking.RankingDatabaseFromFile;
import kr.kwakyc.puzzle.ranking.RankingRecord;

public class Ranking {
	private RankingDatabase rankingDatabase;
	
	private List<RankingRecord> rankingRecords;

	private JFrame frameRanking;
	
	private Comparator timeComparator = new TimeComparator();
	
	public Ranking(){
		init();
		loadData();
		
		makeRankingFrame();
		refreshRanking();
		frameRanking.pack();
	}

	public void init(){
		rankingDatabase = new RankingDatabaseFromFile();
	}
	
	private void loadData() {
		rankingRecords = rankingDatabase.list();
		Collections.sort(rankingRecords, timeComparator);
	}

	class TimeComparator implements Comparator<RankingRecord>{
		public int compare(RankingRecord record1, RankingRecord record2) {
			return record1.getSecond() - record2.getSecond();
		}
	}
	
	
	private void makeRankingFrame() {
		frameRanking = new JFrame();
		frameRanking.setLocation(400, 200);
		frameRanking.setPreferredSize(new Dimension(800, 800));
	}

	private void refreshRanking() {
		Container contentPane = frameRanking.getContentPane();
		contentPane.removeAll();
		
		JPanel rankingPanel = new JPanel();
		rankingPanel.setPreferredSize(new Dimension(200, 600));
		rankingPanel.setLayout(new GridLayout(0, 2));
		
		JLabel name = new JLabel("이름");
		JLabel clearTime = new JLabel("시간");
		
		rankingPanel.add(name);
		rankingPanel.add(clearTime);
		
		for(int i = 0; i < rankingRecords.size(); i++){
			RankingRecord record = rankingRecords.get(i);
			
			JLabel uName = new JLabel(record.getName());
			JLabel uRecord = new JLabel(record.getRecordWithMMSS());
			
			rankingPanel.add(uName);
			rankingPanel.add(uRecord);
		}
		
		contentPane.add(rankingPanel, BorderLayout.CENTER);
		
		frameRanking.setPreferredSize(new Dimension(500, rankingRecords.size() * 30));
	}

	public void show() {
		frameRanking.setVisible(true);
	}
	
	public void saveRankingRecord(String name, int second){
		RankingRecord record = new RankingRecord(name, second);
		rankingRecords.add(record);
		rankingDatabase.insert(record);
		Collections.sort(rankingRecords, timeComparator);
		
		
		refreshRanking();
	}

	public void showRankingInputFrame(int clearSecond) {
		JFrame frameRankingInput = new JFrame("랭킹 정보 입력창");
		frameRankingInput.setLocation(200, 300);
		frameRankingInput.setPreferredSize(new Dimension(500, 700));
		
		JLabel lblName = new JLabel("이름 : ");
		JTextField tfName = new JTextField(30);
		JLabel lblRecord = new JLabel("기록 : " + clearSecond + " 초");
		
		JButton btnSave = new JButton("기록 저장");
		btnSave.addActionListener(new RankingInputActionListener(frameRankingInput, tfName, clearSecond));
		
		Container contentPane = frameRankingInput.getContentPane();
		
		JPanel panel = new JPanel();
		panel.add(lblName);
		panel.add(tfName);
		panel.add(lblRecord);
		panel.add(btnSave);
		
		contentPane.add(panel, BorderLayout.CENTER);
		
		frameRankingInput.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frameRankingInput.pack();
		frameRankingInput.setVisible(true);
	}
	
	public class RankingInputActionListener implements ActionListener{
		private JFrame frameRankingInput;
		private JTextField tfName;
		private int clearSecond;
		
		public RankingInputActionListener(JFrame frameRankingInput, JTextField tfName, int clearSecond){
			this.frameRankingInput = frameRankingInput;
			this.tfName = tfName;
			this.clearSecond = clearSecond;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			saveRankingRecord(tfName.getText(), clearSecond);
			frameRankingInput.dispose();
			frameRanking.setVisible(true);
		}
	}
}
